import pandas as pd
import os
from datetime import datetime, timedelta
import chardet

# 定义目录路径
directory = r"D:\pythonProject\BaiduSyncdisk\LongMoneyTrade\各部分数据\后复权周线"
# 更新输出Excel文件路径
output_excel = os.path.join(directory, "一年内所有发生底背离的股票.xlsx")

# 计算时间范围（从今天开始一年内）
today = datetime.today()
one_year_ago = today - timedelta(days=365)  # 修改为365天
print(f"查找时间范围: {one_year_ago.strftime('%Y-%m-%d')} 至 {today.strftime('%Y-%m-%d')}")

# 存储所有符合条件的记录
all_divergence_records = []

# 遍历目录下的所有CSV文件
for filename in os.listdir(directory):
    if not filename.endswith('.csv'):
        continue

    file_path = os.path.join(directory, filename)
    # 从文件名提取股票代码
    if '周线' in filename and filename.endswith('.csv'):
        stock_code = filename.split('周线')[-1].split('.csv')[0]
    else:
        stock_code = filename.replace('.csv', '')

    try:
        # 尝试使用utf-8编码读取文件
        try:
            df = pd.read_csv(file_path, encoding='utf-8')
        except (UnicodeDecodeError, pd.errors.ParserError):
            # 如果utf-8失败，检测文件编码
            with open(file_path, 'rb') as f:
                raw_data = f.read(10000)
                result = chardet.detect(raw_data)
                encoding = result['encoding']
            df = pd.read_csv(file_path, encoding=encoding)

        # 检查必要的列是否存在
        required_columns = ['交易结束日期', '是否背离']
        if not all(col in df.columns for col in required_columns):
            print(f"  跳过 {filename}: 缺少必要列")
            continue

        # 确保日期列是datetime类型
        df['交易结束日期'] = pd.to_datetime(df['交易结束日期'], errors='coerce')

        # 筛选符合条件的记录
        # 1. 日期在最近一年内
        # 2. "是否背离"列是"底背离"
        mask = (df['交易结束日期'] >= one_year_ago) & (df['交易结束日期'] <= today)
        mask = mask & (df['是否背离'] == '底背离')  # 仅筛选底背离

        # 直接筛选出符合条件的记录
        divergence_df = df.loc[mask].copy()

        if not divergence_df.empty:
            print(f"  在 {stock_code} 中找到 {len(divergence_df)} 条底背离记录")

            # 添加股票代码信息
            divergence_df.loc[:, '股票代码'] = stock_code

            # 添加到总列表
            all_divergence_records.append(divergence_df)

    except Exception as e:
        print(f"  处理 {filename} 时出错: {str(e)}")

# 合并所有记录
if all_divergence_records:
    final_df = pd.concat(all_divergence_records, ignore_index=True)

    # 保存到Excel
    final_df.to_excel(output_excel, index=False)
    print(f"\n找到 {len(final_df)} 条底背离记录，已保存到: {output_excel}")

    # 显示预览
    print("\n预览前5条记录:")
    if '股票代码' in final_df.columns and '交易结束日期' in final_df.columns and '是否背离' in final_df.columns:
        print(final_df[['股票代码', '交易结束日期', '是否背离']].head())
    else:
        print(final_df.head().to_string(index=False))
else:
    print("\n未找到任何符合条件的底背离记录")

print("\n处理完成!")